package com.liwq.base.bolckqueue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author : liwq
 * @version : 1.0
 * @date : 2023/2/21
 **/
public class SynchronousQueueDemo {
    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue = new SynchronousQueue<>();

        new Thread(()->{
            try {
                System.out.println("放进去1");
                blockingQueue.put("1");
                System.out.println("放进去2");
                blockingQueue.put("2");
                System.out.println("放进去3");
                blockingQueue.put("3");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"AA").start();

        new Thread(()->{
            try {

                TimeUnit.SECONDS.sleep(2);

                System.out.println("拿1");
                blockingQueue.take();
                TimeUnit.SECONDS.sleep(2);
                System.out.println("拿2");
                blockingQueue.take();
                TimeUnit.SECONDS.sleep(2);
                System.out.println("拿3");
                blockingQueue.take();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"AA").start();

    }
}
